package news

import (
	"parking-lot-go-admin/config"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/dingtalk"

	"github.com/gin-gonic/gin"
)

func NewsList(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var dingtalkNewsStruct dingtalk.DingtalkNewsStruct
	var dingtalkNewsData []dingtalk.DingtalkNewsStruct
	model.GetModelData(
		model.Db,
		dingtalkNewsStruct.TableName(),
		&dingtalkNewsData,
		model.QueryOptions{
			Conditions: where,
			OrderBy:    "id desc",
			Page:       page,
			PageSize:   pageSize,
			Debug:      true,
		},
	)

	systemSection := config.Cfg.Section("SYSTEM")
	imgUrl := systemSection.Key("IMAGE_URL").String()

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range dingtalkNewsData {
		returnData = append(returnData, map[string]interface{}{
			"id":         v.Id,
			"class_id":   v.ClassId,
			"title":      v.Title,
			"introduce":  v.Introduce,
			"content":    v.Content,
			"image_path": imgUrl + v.ImagePath,
			"sort":       v.Sort,
			"clicks":     v.Clicks,
			"is_top":     v.IsTop,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		dingtalkNewsStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
